package com.tools;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;

public class BitmapUtile {

	static BitmapUtile mBitmapUtile;

	public static BitmapUtile getBitmapUtile() {

		if (mBitmapUtile == null) {
			mBitmapUtile = new BitmapUtile();
		}

		return mBitmapUtile;

	}

	public Bitmap decodeBitmap(int resId, Resources res, int desW, int desH) {

		BitmapFactory.Options opts = new BitmapFactory.Options();
		opts.inJustDecodeBounds = true;// 设置成了true,不占用内存，只获取bitmap宽高
		InputStream is = res.openRawResource(resId);
		BitmapFactory.decodeStream(is, null, opts);
		opts.inSampleSize = computeSampleSize(opts, -1, desW * desH);
		opts.inJustDecodeBounds = false;// 这里一定要将其设置回false，因为之前我们将其设置成了true
		opts.inPurgeable = true;
		opts.inInputShareable = true;
		opts.inDither = false;
		opts.inPurgeable = true;
		opts.inTempStorage = new byte[1024];
		opts.inPreferredConfig = Bitmap.Config.RGB_565;
		Bitmap bmp = null;
		bmp = BitmapFactory.decodeStream(is, null, opts);

		// Log.d("decodeBitmap", bmp.getHeight() + " " + bmp.getWidth() + " "
		// + bmp.getByteCount());
		return bmp;

	}

	public Bitmap decodeBitmap(String path, int desW, int desH) {

		try {

			BitmapFactory.Options opts = new BitmapFactory.Options();
			opts.inJustDecodeBounds = true;// 设置成了true,不占用内存，只获取bitmap宽高
			InputStream is = new FileInputStream(new File(path));

			BitmapFactory.decodeStream(is, null, opts);
			opts.inSampleSize = computeSampleSize(opts, -1, desW * desH);
			
			Log.d("decodeBitmap", "h=" + opts.outHeight + "  w="
					+ opts.outWidth+" inSampleSize= "+opts.inSampleSize);
			opts.inJustDecodeBounds = false;// 这里一定要将其设置回false，因为之前我们将其设置成了true
			opts.inPurgeable = true;
			opts.inInputShareable = true;
			opts.inDither = false;
			opts.inPurgeable = true;
			opts.inTempStorage = new byte[1024];
			opts.inPreferredConfig = Bitmap.Config.RGB_565;
			Bitmap bmp = null;
			bmp = BitmapFactory.decodeFile(path, opts);

			Log.d("decodeBitmap", bmp.getHeight() + " " + bmp.getWidth() + " "
					+ bmp.getByteCount());
			return bmp;
		} catch (FileNotFoundException e) {
			e.printStackTrace();

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	private int computeSampleSize(BitmapFactory.Options options,
			int minSideLength, int maxNumOfPixels) {
		int initialSize = computeInitialSampleSize(options, minSideLength,
				maxNumOfPixels);

		int roundedSize;
		if (initialSize <= 8) {
			roundedSize = 1;
			while (roundedSize < initialSize) {
				roundedSize <<= 1;
			}
		} else {
			roundedSize = (initialSize + 7) / 8 * 8;
		}

		return roundedSize;
	}

	private int computeInitialSampleSize(BitmapFactory.Options options,
			int minSideLength, int maxNumOfPixels) {
		double w = options.outWidth;
		double h = options.outHeight;

		int lowerBound = (maxNumOfPixels == -1) ? 1 : (int) Math.ceil(Math
				.sqrt(w * h / maxNumOfPixels));
		int upperBound = (minSideLength == -1) ? 128 : (int) Math.min(
				Math.floor(w / minSideLength), Math.floor(h / minSideLength));

		if (upperBound < lowerBound) {
			return lowerBound;
		}

		if ((maxNumOfPixels == -1) && (minSideLength == -1)) {
			return 1;
		} else if (minSideLength == -1) {
			return lowerBound;
		} else {
			return upperBound;
		}
	}

}
